package cn.edu.neusoft.yuejian.adapter;


import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;



import com.squareup.picasso.Picasso;
import com.squareup.picasso.Transformation;

import java.util.List;

import cn.edu.neusoft.yuejian.R;
import cn.edu.neusoft.yuejian.activity.DsInfoActivity;
import cn.edu.neusoft.yuejian.activity.HdInfoActivity;
import cn.edu.neusoft.yuejian.bean.activity;
import cn.edu.neusoft.yuejian.bean.master;


/**
 * Created by Administrator on 2017/9/25.
 */

public class dsadapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context mContext;
    private List<master> mDataList;
    private LayoutInflater mLayoutInflater;
    public dsadapter(Context mContext, List<master> mDataList) {
        this.mContext = mContext;
        this.mDataList = mDataList;
        mLayoutInflater = LayoutInflater.from(mContext);
    }

    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = mLayoutInflater.from(parent.getContext()).inflate(R.layout.cardview_ds, parent, false);
        return new ViewHolder(v);

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final master entity = (master) mDataList.get(position);
        if (null == entity)
            return;
        ViewHolder viewHolder = (ViewHolder) holder;
        viewHolder.ds_name.setText(entity.getActcate_name()+"大师"+entity.getRealname());
        viewHolder.ds_scs.setText(entity.getHits());
//        String a=entity.getThumb();
//        Log.d("ywfio","efbh"+a);
        Picasso.with(mContext)
                .load("http://172.24.10.175/workout/Uploads/"+entity.getThumb())
                .error(R.drawable.wu)
                .transform(new CropCircleTransformation())
                .into(viewHolder.ds_pic);
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(mContext,DsInfoActivity.class);

                intent.putExtra("id",entity.getId());
                intent.putExtra("Club_name",entity.getClub_name());
                intent.putExtra("City_name",entity.getCity_name());
                intent.putExtra("Province_name",entity.getProvince_name());
                intent.putExtra("Introduction",entity.getIntroduction());
                intent.putExtra("Thumb",entity.getThumb());
                mContext.startActivity(intent);
            }
        });

    }

    @Override
    public int getItemCount() {

        return mDataList.size();

    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        TextView ds_name;
        ImageView ds_pic;
        TextView ds_scs;
        public ViewHolder(View itemView) {
            super(itemView);
            ds_name = (TextView) itemView.findViewById(R.id.ds_name);
            ds_pic = (ImageView) itemView.findViewById(R.id.ds_pic);
            ds_scs = (TextView) itemView.findViewById(R.id.ds_scs);
        }
    }
    public class CropCircleTransformation implements Transformation {

        @Override public Bitmap transform(Bitmap source) {
            int size = Math.min(source.getWidth(), source.getHeight());

            int width = (source.getWidth() - size) / 2;
            int height = (source.getHeight() - size) / 2;

            Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);

            Canvas canvas = new Canvas(bitmap);
            Paint paint = new Paint();
            BitmapShader shader =
                    new BitmapShader(source, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP);
            if (width != 0 || height != 0) {
                // source isn't square, move viewport to center
                Matrix matrix = new Matrix();
                matrix.setTranslate(-width, -height);
                shader.setLocalMatrix(matrix);
            }
            paint.setShader(shader);
            paint.setAntiAlias(true);

            float r = size / 2f;
            canvas.drawCircle(r, r, r, paint);

            source.recycle();

            return bitmap;
        }

        @Override public String key() {
            return "CropCircleTransformation()";
        }
    }
}
